/************************************************************************
 * The code is owned by mainframer.cn and Shang Tao
 * Without aproval of mainframer.cn and Shang Tao, removing the copyright
 * infomation is regarded as invassion of proprietary copyright
 ***********************************************************************/
package com.mr.qa.taglib;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.mr.qa.bo.Question;
import com.mr.qa.bo.QuestionCategory;


/**
 * This taglib makes category navigation links
 */
public class QuestionCategoryNavigationLinkTaglib extends BodyTagSupport
{
    private static final Log log = LogFactory.getLog(QuestionCategoryNavigationLinkTaglib.class); 
 
   
    //"Q" Question object
    //"C" Category object
    private String objType;
    
    public QuestionCategoryNavigationLinkTaglib()
    {
        super();
    }

    public int doStartTag() throws JspException
    {
        try
        {
            HttpServletRequest request = (HttpServletRequest)this.pageContext.getRequest();        
            String outString = "";
            if(objType.equals("C"))//make navigation for category view screen
            {
                QuestionCategory qc = (QuestionCategory)request.getAttribute("questioncategory");
                if(qc != null)
                {
                    outString = makeLinks(qc,request);
                }
            }
            if(objType.equals("Q"))//make navigation for question view screen
            {
                Question q = (Question)request.getAttribute("questiono");
                if(q != null)
                {
                
                    outString = makeLinks(q.getMrIaskCategory(),request);
                }
            }
            JspWriter out = pageContext.getOut();
            out.println(outString);
        }
        catch (Exception e)
        { 
            throw new JspException(e);
        }
        return this.EVAL_PAGE;
    }    

    public int doEndTag() throws JspTagException 
    {
        return 0;
    }

    
    private String makeLinks(Question question,HttpServletRequest request)
    {
        StringBuffer sb = new StringBuffer();
        StringBuffer sb3 = new StringBuffer();
        StringBuffer sb2 = new StringBuffer();
        StringBuffer sb1 = new StringBuffer();
        String action = request.getContextPath()+"/question.mrqa?action=list&catId=";
        String actionLife = request.getContextPath()+"/life.mrqa?action=list&catId=";
        String actionTech = request.getContextPath()+"/tech.mrqa?action=list&catId=";
        if(question != null)
        {
            QuestionCategory qc3 = question.getMrIaskCategory();
            if(qc3 != null)
            {
                sb3.append("&nbsp;>&nbsp;");
                sb3.append("<a href='");
                if(qc3.getType() == 0){
                	sb3.append(action);
                }else if(qc3.getType() == 1){
                	sb3.append(actionLife);
                }else if(qc3.getType() == 2){
                	sb3.append(actionTech);
                }
                sb3.append(qc3.getId());
                sb3.append("'>");
                sb3.append(qc3.getName());
                sb3.append("</a>");
                QuestionCategory qc2 = qc3.getMrIaskCategory();
                if(qc2 != null)
                {
                    sb2.append("&nbsp;>&nbsp;");
                    sb2.append("<a href='");
                    if(qc2.getType() == 0){
                    	sb2.append(action);
                    }else if(qc2.getType() == 1){
                    	sb2.append(actionLife);
                    }else if(qc2.getType() == 2){
                    	sb2.append(actionTech);
                    }
                    sb2.append(qc2.getId());
                    sb2.append("'>");
                    sb2.append(qc2.getName());
                    sb2.append("</a>");
                    QuestionCategory qc1 = qc2.getMrIaskCategory();
                    if(qc1 != null)
                    {
                        sb1.append("&nbsp;>&nbsp;");
                        sb1.append("<a href='");
                        if(qc1.getType() == 0){
                        	sb1.append(action);
                        }else if(qc1.getType() == 1){
                        	sb1.append(actionLife);
                        }else if(qc1.getType() == 2){
                        	sb1.append(actionTech);
                        }
                        sb1.append(qc1.getId());
                        sb1.append("'>");
                        sb1.append(qc1.getName());
                        sb1.append("</a>");
                    }    
                }
            }
        }
        if(sb1.toString().length() > 0)
            sb.append(sb1);
        if(sb2.toString().length() > 0)
            sb.append(sb2);
        if(sb3.toString().length() > 0)
            sb.append(sb3);
        return sb.toString();
    }

    private String makeLinks(QuestionCategory qc,HttpServletRequest request)
    {
        StringBuffer sb = new StringBuffer();
        StringBuffer sb3 = new StringBuffer();
        StringBuffer sb2 = new StringBuffer();
        StringBuffer sb1 = new StringBuffer();
        String action = request.getContextPath()+"/question.mrqa?action=list&catId=";
        String actionLife = request.getContextPath()+"/life.mrqa?action=list&catId=";
        String actionTech = request.getContextPath()+"/tech.mrqa?action=list&catId=";
        
        if(qc != null)
        {
            if(qc != null)
            {
                sb3.append("&nbsp;>&nbsp;");
                sb3.append("<a href='");
                if(qc.getType() == 0){
                	sb3.append(action);
                }else if(qc.getType() == 1){
                	sb3.append(actionLife);
                }else if(qc.getType() == 2){
                	sb3.append(actionTech);
                }
                sb3.append(qc.getId());
                sb3.append("'>");
                sb3.append(qc.getName());
                sb3.append("</a>");
                QuestionCategory qc2 = qc.getMrIaskCategory();
                if(qc2 != null)
                {
                    sb2.append("&nbsp;>&nbsp;");
                    sb2.append("<a href='");
                    if(qc2.getType() == 0){
                    	sb2.append(action);
                    }else if(qc2.getType() == 1){
                    	sb2.append(actionLife);
                    }else if(qc2.getType() == 2){
                    	sb2.append(actionTech);
                    }
                    sb2.append(qc2.getId());
                    sb2.append("'>");
                    sb2.append(qc2.getName());
                    sb2.append("</a>");
                    QuestionCategory qc1 = qc2.getMrIaskCategory();
                    if(qc1 != null)
                    {
                        sb1.append("&nbsp;>&nbsp;");
                        sb1.append("<a href='");
                        if(qc1.getType() == 0){
                        	sb1.append(action);
                        }else if(qc1.getType() == 1){
                        	sb1.append(actionLife);
                        }else if(qc1.getType() == 2){
                        	sb1.append(actionTech);
                        }
                        sb1.append(qc1.getId());
                        sb1.append("'>");
                        sb1.append(qc1.getName());
                        sb1.append("</a>");
                    }    
                }
            }
        }
        if(sb1.toString().length() > 0)
            sb.append(sb1);
        if(sb2.toString().length() > 0)
            sb.append(sb2);
        if(sb3.toString().length() > 0)
            sb.append(sb3);
            
        return sb.toString();
    }


    public void setObjType(String objType)
    {
        this.objType = objType;
    }
    
    public String getObjType()
    {
        return objType;
    }
}
